package com.xhu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhu.dao.MissionMapper;
import com.xhu.entity.Mission;
import com.xhu.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@RestController
@Slf4j
public class MissionController {

    @Autowired
    private MissionMapper missionMapper;
    @Autowired
    private HttpSession session;

    @GetMapping("/getMissions/{pageNum}")
    public PageInfo getMissions(@PathVariable("pageNum") int pageNum){
        User userInfo = (User) session.getAttribute("userInfo");

        PageHelper.startPage(pageNum,3);
        List<Mission> missions = missionMapper.selectAll(userInfo.getUserAccount());

        PageInfo<Mission> missionPageInfo = new PageInfo<>(missions);

        return missionPageInfo;
    }

    @GetMapping("/downLoadMissionFile")
    public void downLoadMissionFile(String fileLocation, HttpServletResponse response)
            throws UnsupportedEncodingException {

        File targetFile = new File(fileLocation+".result");

        log.debug(fileLocation);

        // 如果文件名存在，则进行下载
        if (targetFile.exists()) {

            // 配置文件下载
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            // 下载文件能正常显示中文
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("downLoadTest.html", "UTF-8"));

            // 实现文件下载
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(targetFile);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                log.debug("Download the song successfully!");
            }
            catch (Exception e) {
                e.printStackTrace();
                log.debug("Download the song failed!");
            }
            finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
